package com.jsmy.softapp.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jsmy.softapp.R;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.bean.VersionBean;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.SettingPresenter;
import com.jsmy.softapp.util.CacheDataManager;
import com.jsmy.softapp.util.DialogUtils;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.util.VersionUtil;
import com.jsmy.softapp.view.SettingView;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

@Route(path = AR.A_SETTING)
public class SettingActivity extends BaseActivity<SettingView, SettingPresenter> implements SettingView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    VersionBean.DataBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingActivity.this.finish();
            }
        });
        tvVersion.setText(VersionUtil.getVersionName(this));

//        tvCache.setText(CacheDataManager.getTotalCacheSize(this));
        CacheDataManager.getTotalCacheSize(this).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                tvCache.setText(s);
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getversioninfo();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.APP_VERSION_INFO:
                bean = ((VersionBean) object).getData();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        presenter.model.disposable.clear();
        super.onDestroy();
    }

    @OnClick({R.id.btn_msg, R.id.btn_about, R.id.btn_users, R.id.btn_psd, R.id.btn_cahe, R.id.btn_version, R.id.btn_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_msg:
            case R.id.btn_about:
            case R.id.btn_users:
            case R.id.btn_psd:
                ToastUtil.showShort(this, "设计开发中");
                break;
            case R.id.btn_cahe:
                DialogUtils.showCacheDialog(this, tvCache.getText().toString(), tvCache);
                break;
            case R.id.btn_version:
                if (VersionUtil.getVersionName(this).equals(bean.getVersionname())) {
                    ToastUtil.showShort(this, "当前已是最新版本！");
                } else {
                    DialogUtils.showVersionDialog(this, bean.getVersionname(), bean.getVermsg(), bean.getVersionurl());
                }
                break;
            case R.id.btn_logout:
                DialogUtils.showLogBackDialog(this);
                break;
        }
    }
}
